package org.example.leetcpde;

import org.junit.jupiter.api.Test;

public class z {

    @Test
    void convert() {
        String s = "PAYPALISHIRING";
        System.out.println(convert(s, 3));
    }

    public String convert(String s,
                          int numRows) {
        if (s == null) {
            return null;
        }

        if (s.length() == 1 || numRows <= 1) {
            return s;
        }

        StringBuilder[] builders = new StringBuilder[numRows];
        for (int i = 0; i < numRows; i++) {
            builders[i] = new StringBuilder();
        }

        char[] chars = s.toCharArray();
        int t = (numRows << 1) - 2;
        for (int i = 0, x = 0; i < chars.length; i++) {
            builders[x].append(chars[i]);
            if (i % t < numRows - 1) {
                x++;
            } else {
                x--;
            }
        }

        StringBuilder res = new StringBuilder();
        for (StringBuilder builder : builders) {
            res.append(builder);
        }

        return res.toString();
    }
}
